package cc.yugu.yhdw.webserver.web.controller.security.basic;
import cc.yugu.yhdw.common.annotation.ViewModel;
import cc.yugu.yhdw.common.constant.ConstEnum;
import cc.yugu.yhdw.common.domain.basic.*;
import cc.yugu.yhdw.common.entity.json.ExtResult;
import cc.yugu.yhdw.common.entity.json.PageResult;
import cc.yugu.yhdw.webserver.constant.AppConstEnum;
import cc.yugu.yhdw.webserver.entity.SessionUser;
import cc.yugu.yhdw.webserver.service.basic.AlipayPayOrderService;
import cc.yugu.yhdw.webserver.service.basic.CustomerForegiftOrderService;
import cc.yugu.yhdw.webserver.service.basic.CustomerForegiftService;
import cc.yugu.yhdw.webserver.service.basic.WeixinPayOrderService;
import cc.yugu.yhdw.webserver.web.controller.security.SecurityController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
/**
 * 客户押金设置请求
 *
 * @Date:Created in 2018/8/1 16:41
 * @Author:
 * @Modified By: zxj 2018/8/1 16:41
 */
@Controller
@RequestMapping(value = "/security/basic/customer_foregift")
public class CustomerForegiftController extends SecurityController {
    @Autowired
    CustomerForegiftService customerForegiftService;

    /**
     * 首页
     * @Param [model]
     * @Return void
     * @Author zxj 2018/8/1
     */
    @RequestMapping(value = "index.htm")
    public void index(Model model) {
        model.addAttribute(MENU_CODE_NAME, AppConstEnum.Menu.MENU_03_16.getValue());
    }
    /**
     * 分页查询客户押金记录信息
     * @Param [search, request]
     * @Return cc.yugu.yhdw.common.entity.json.PageResult
     * @Author zxj 2018/8/2
     */
    @RequestMapping("page.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public PageResult page(CustomerForegift search, HttpServletRequest request) {
        // 判断是否是 选择所有
        if (search.getAgentId() != null && search.getAgentId() == 0) {
            search.setAgentId(null);
        }
        if (search.getAgencyId() != null && search.getAgencyId() == 0) {
            search.setAgencyId(null);
        }
        // 如果是运营商登录
        if (getSessionUser(request).getType() == User.Type.AGENT.getValue()) {
            search.setAgentId(getSessionUser(request).getAgentId());
        } else if (getSessionUser(request).getType() == User.Type.AGENCY.getValue()) {
            search.setAgencyId(getSessionUser(request).getAgencyId());
        }
        return PageResult.successResult(customerForegiftService.findPage(search));
    }
    /**
     * 查看页面
     * @Param [model, id]
     * @Return java.lang.String
     * @Author zxj 2018/8/2
     */
    @RequestMapping(value = "view.htm")
    public String view(Model model, int id) {
        CustomerForegift entity = customerForegiftService.find(id);
        if (entity == null) {
            return SEGMENT_RECORD_NOT_FOUND;
        } else {
            model.addAttribute("entity", entity);
        }
        model.addAttribute("id", id);
        return "/security/basic/customer_foregift/view";
    }

    /**
     * 新建页面
     * @Param [model]
     * @Return java.lang.String
     * @Author zxj 2018/8/1
     */
    @RequestMapping("add.htm")
    public void add() {}
    /**
     * 新建请求
     * @Param [entity, request]
     * @Return cc.yugu.yhdw.common.entity.json.ExtResult
     * @Author zxj 2018/8/2
     */
    @RequestMapping("create.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public ExtResult create(CustomerForegift entity, HttpServletRequest request) {
        // 运营商新建代理商,代理商没有权限新建
        if (getSessionUser(request).getType() == User.Type.AGENT.getValue()) {
            entity.setAgentId(getSessionUser(request).getAgentId());
        }
        return customerForegiftService.create(entity);
    }
    /**
     * 修改页面
     * @Param [model, id]
     * @Return java.lang.String
     * @Author zxj 2018/8/2
     */
    @RequestMapping(value = "edit.htm")
    public String edit(Model model, int id) {
        CustomerForegift entity = customerForegiftService.find(id);
        if(entity == null) {
            return SEGMENT_RECORD_NOT_FOUND;
        } else {
            model.addAttribute("entity", entity);
        }
        return "/security/basic/customer_foregift/edit";
    }
    /**
     * 修改请求（假设代理商没有修改的权限）
     * @Param [entity]
     * @Return cc.yugu.yhdw.common.entity.json.ExtResult
     * @Author zxj 2018/8/2
     */
    @RequestMapping("update.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public ExtResult update(CustomerForegift entity) {
        return customerForegiftService.update(entity);
    }
    /**
     * 删除请求
     * @Param [id]
     * @Return cc.yugu.yhdw.common.entity.json.ExtResult
     * @Author zxj 2018/8/2
     */
    @RequestMapping("delete.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public ExtResult delete(int id) {
        return customerForegiftService.delete(id);
    }

}
